package com.spsoft.vatoutinv.controller.matchflow;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.spsoft.core.annotation.AnonymousAccess;
import com.spsoft.core.annotation.Log;
import com.spsoft.core.controller.BaseController;
import com.spsoft.core.enums.LogActionType;
import com.spsoft.core.enums.LogMenuEnum;
import com.spsoft.core.model.Result;
import com.spsoft.vatoutinv.enums.CommonEnum;
import com.spsoft.vatoutinv.model.dto.matchflow.VmsItemFlowPageDto;
import com.spsoft.vatoutinv.model.dto.matchflow.VmsMatchFlowPageDto;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchFlowApplyVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchInvoiceInfoVo;
import com.spsoft.vatoutinv.model.vo.matchflow.VmsMatchRecordVo;
import com.spsoft.vatoutinv.model.vo.vmstransactionflowinvoice.VmsTransactionFlowInvoicePageVo;
import com.spsoft.vatoutinv.service.matchflow.VmsMatchFlowRevokeService;
import com.spsoft.vatoutinv.service.matchflow.VmsMatchFlowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 手工匹配流水撤销 前端控制器
 * </p>
 *
 * @author chenxudong
 * @since 2024-06-25
 */
@RestController
@Api(tags = {"手工匹配流水撤销管理"})
@RequestMapping("/matchFlowRevoke")
@RequiredArgsConstructor
@Validated
@Slf4j
public class VmsMatchFlowRevokeController extends BaseController {

    public static final String LOG_MSG = "(手工匹配流水撤销控制层)";

    private final VmsMatchFlowService vmsMatchFlowService;

    private final VmsMatchFlowRevokeService vmsMatchFlowRevokeService;

    @ApiOperation("手工匹配流水撤销主页面-分页查询")
    @AnonymousAccess(value = "/pageApply", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "手工匹配流水撤销主页面-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    public Result<IPage<VmsMatchFlowApplyVo>> pageApply(@RequestBody @Validated VmsMatchFlowPageDto pageDto) {
        pageDto.setApplyType(CommonEnum.MATCH_FLOW_TYPE_1.getKey());
        log.info("{}手工匹配流水撤销主页面-分页查询入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowRevokeService.pageApply(pageDto);
    }

    @ApiOperation("手工匹配流水撤销主页面-数据导出")
    @AnonymousAccess(value = "/export", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "手工匹配流水撤销主页面-数据导出", type = LogActionType.DOWNLOAD, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    public void export(@RequestBody @Validated VmsMatchFlowPageDto pageDto, HttpServletResponse response) {
        pageDto.setApplyType(CommonEnum.MATCH_FLOW_TYPE_1.getKey());
        log.info("{}手工匹配流水撤销主页面-数据导出入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        vmsMatchFlowRevokeService.export(pageDto, response);
    }

    @ApiOperation("新增子界面-快速添加")
    @AnonymousAccess(value = "/fastAdd", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "新增子界面-快速添加", type = LogActionType.ADD, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fpdm", value = "发票代码", dataTypeClass = String.class, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "fphm", value = "发票号码", dataTypeClass = String.class, dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "applyId", value = "审批流id", dataTypeClass = Long.class, dataType = "Long", paramType = "query", required = true)
    })
    public Result<VmsMatchInvoiceInfoVo> fastAdd(@RequestParam(value = "fpdm", required = false) String fpdm,
                                                 @RequestParam(value = "fphm") String fphm,
                                                 @RequestParam(value = "applyId") Long applyId) {
        log.info("{}新增子界面-快速添加，发票代码：{}，发票号码：{}，审批流id：{}", LOG_MSG, fpdm, fphm, applyId);
        return vmsMatchFlowRevokeService.fastAdd(fpdm, fphm, applyId, getUser());
    }

    @ApiOperation("查询匹配流水撤销详情-流程使用")
    @AnonymousAccess(value = "/getApplyDetail", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "查询匹配流水撤销详情-流程使用", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyId", value = "审批流id", dataTypeClass = Long.class, dataType = "Long", required = true)
    })
    public Result<VmsMatchInvoiceInfoVo> getApplyDetail(@RequestParam(value = "applyId") Long applyId) {
        log.info("{}查询匹配流水撤销详情-流程使用，审批流id：{}", LOG_MSG, applyId);
        return vmsMatchFlowRevokeService.getApplyDetail(applyId);
    }

    @ApiOperation("查询匹配流水撤销详情-页面使用")
    @AnonymousAccess(value = "/getMatchDetail", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "查询匹配流水撤销详情-页面使用", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "matid", value = "匹配流水记录id", dataTypeClass = Long.class, dataType = "Long", required = true)
    })
    public Result<VmsMatchRecordVo> getMatchDetail(@RequestParam(value = "matid") Long matid) {
        log.info("{}查询匹配流水撤销详情-页面使用，匹配流水记录id：{}", LOG_MSG, matid);
        return vmsMatchFlowService.getMatchDetail(matid);
    }

    @ApiOperation("根据发票明细查询已匹配的交易流水-分页查询")
    @AnonymousAccess(value = "/queryFlowByItem", method = RequestMethod.POST, permissionCode = "")
    @Log(value = "根据发票明细查询已匹配的交易流水-分页查询", type = LogActionType.SELECT, menuName = LogMenuEnum.VMS_MATCH_FLOW_REVOKE)
    public Result<IPage<VmsTransactionFlowInvoicePageVo>> queryFlowByItem(@RequestBody @Validated VmsItemFlowPageDto pageDto) {
        pageDto.setDeptCode(getDeptCode());
        log.info("{}根据发票明细查询已匹配的交易流水，入参：{}", LOG_MSG, JSONObject.toJSONString(pageDto));
        return vmsMatchFlowService.queryFlowByItem(pageDto);
    }
}

